package com.dime.basic.service.impl;

import java.util.List;

import com.dime.basic.api.domain.DimeBasedisease;
import com.dime.basic.api.domain.DimeBasehealthadvice;
import com.dime.basic.mapper.DimeBasediseaseMapper;
import com.dime.basic.mapper.DimeBasegroupMapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dime.basic.mapper.DimeBasehealthadviceMapper;

import com.dime.basic.service.IDimeBasehealthadviceService;

/**
 * 健康建议Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-28
 */
@Service
public class DimeBasehealthadviceServiceImpl implements IDimeBasehealthadviceService
{
    @Autowired
    private DimeBasehealthadviceMapper dimeBasehealthadviceMapper;

    @Autowired
    private DimeBasegroupMapper dimeBasegroupMapper;

    @Autowired
    private DimeBasediseaseMapper dimeBasediseaseMapper;

    /**
     * 查询健康建议
     *
     * @param id 健康建议主键
     * @return 健康建议
     */
    @Override
    public DimeBasehealthadvice selectDimeBasehealthadviceById(String id)
    {
        return dimeBasehealthadviceMapper.selectDimeBasehealthadviceById(id);
    }

    /**
     * 查询健康建议列表
     *
     * @param dimeBasehealthadvice 健康建议
     * @return 健康建议
     */
    @Override
    public List<DimeBasehealthadvice> selectDimeBasehealthadviceList(DimeBasehealthadvice dimeBasehealthadvice)
    {
        return dimeBasehealthadviceMapper.selectDimeBasehealthadviceList(dimeBasehealthadvice);
    }

    /**
     * 新增健康建议
     *
     * @param dimeBasehealthadvice 健康建议
     * @return 结果
     */
    @Override
    public int insertDimeBasehealthadvice(DimeBasehealthadvice dimeBasehealthadvice)
    {
        dimeBasehealthadvice.setId(IdUtils.fastUUID());
        dimeBasehealthadvice.setCreateTime(DateUtils.getNowDate());
        dimeBasehealthadvice.setCreateBy(SecurityUtils.getUsername());
        setDimeBasehealthadvice(dimeBasehealthadvice);
        return dimeBasehealthadviceMapper.insertDimeBasehealthadvice(dimeBasehealthadvice);
    }

    /**
     * 修改健康建议
     *
     * @param dimeBasehealthadvice 健康建议
     * @return 结果
     */
    @Override
    public int updateDimeBasehealthadvice(DimeBasehealthadvice dimeBasehealthadvice)
    {
        dimeBasehealthadvice.setUpdateBy(SecurityUtils.getUsername());
        dimeBasehealthadvice.setUpdateTime(DateUtils.getNowDate());
        setDimeBasehealthadvice(dimeBasehealthadvice);
        return dimeBasehealthadviceMapper.updateDimeBasehealthadvice(dimeBasehealthadvice);
    }

    /**
     * 批量删除健康建议
     *
     * @param ids 需要删除的健康建议主键
     * @return 结果
     */
    @Override
    public int deleteDimeBasehealthadviceByIds(String[] ids)
    {
        return dimeBasehealthadviceMapper.deleteDimeBasehealthadviceByIds(ids);
    }

    /**
     * 删除健康建议信息
     *
     * @param id 健康建议主键
     * @return 结果
     */
    @Override
    public int deleteDimeBasehealthadviceById(String id)
    {
        return dimeBasehealthadviceMapper.deleteDimeBasehealthadviceById(id);
    }

    @Override
    public List<DimeBasehealthadvice> selectDimeBasehealthadviceBygroupIds(String[] groupids) {

        return dimeBasehealthadviceMapper.selectDimeBasehealthadviceBygroupIds(groupids);
    }

    private void setDimeBasehealthadvice(DimeBasehealthadvice dimeBasehealthadvice){
        if(StringUtils.isNotEmpty(dimeBasehealthadvice.getGroupid())){
            dimeBasehealthadvice.setGroupname(dimeBasegroupMapper.selectDimeBasegroupById(dimeBasehealthadvice.getGroupid()).getGroupName());
        }
        if(StringUtils.isNotEmpty(dimeBasehealthadvice.getCode())){
            DimeBasedisease dimeBasedisease = new DimeBasedisease();
            dimeBasedisease.setCode(dimeBasehealthadvice.getCode());
            DimeBasedisease dimeBasedisease1 = dimeBasediseaseMapper.selectDimeBasediseaseList(dimeBasedisease).stream().findFirst().orElse(null);
            if (dimeBasedisease1 != null){
                dimeBasehealthadvice.setDiseasename(dimeBasedisease1.getName());
            }
        }
    }
}
